<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="文件分类" prop="fileType">
          <el-select v-model="queryParams.fileType" placeholder="全部分类" class="!w-32">
            <el-option label="全部分类" value="" />
            <el-option label="制度文件" value="1" />
            <el-option label="模板文件" value="2" />
          </el-select>
        </el-form-item>

        <el-form-item label="文件名称" prop="fileName">
          <el-input
            v-noSpace
            v-model="queryParams.fileName"
            placeholder="搜索文件名称"
            class="w-32"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          <el-button
            type="primary"
            class="!rounded-button whitespace-nowrap"
            @click="dialogVisible = true"
          >
            <el-icon class="mr-2"><Plus /></el-icon>
            上传文件
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="fileName" label="文件名称" />
        <el-table-column prop="fileType" label="文件分类" />
        <el-table-column prop="department" label="使用部门" />
        <el-table-column prop="publisher" label="责任人" />
        <el-table-column prop="publishTime" label="发布时间" />
        <el-table-column prop="effectiveTime" label="生效时间" />
        <el-table-column prop="expiryTime" label="失效时间" />
        <el-table-column prop="fileStatus" label="状态">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="info" size="small" class="rounded-full">
              待生效
            </el-tag>
            <el-tag
              v-else-if="scope.row.status === 1"
              type="success"
              size="small"
              class="rounded-full"
            >
              生效中
            </el-tag>
            <el-tag v-else type="warning" size="small" class="rounded-full">已作废</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="附件" prop="attachment">
          <template #default="{ row }">
            <el-button link type="primary" @click="downloadFile(row)">
              <el-icon class="mr-1"><Document /></el-icon>
              {{ row.fileName }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              v-if="scope.row.status !== '已作废'"
              link
              type="danger"
              @click="voidFile(scope.row)"
            >
              作废
            </el-button>
            <span v-else class="text-gray-400">已作废</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <el-dialog v-model="dialogVisible" title="文件上传" width="60%">
      <el-form ref="formRef" :model="formData" label-width="120px">
        <el-form-item label="文件分类" prop="fileType">
          <el-select v-model="formData.fileType" placeholder="请选择文件分类" class="w-full">
            <el-option label="制度文件" value="1" />
            <el-option label="模板文件" value="2" />
            <el-option label="其他文件" value="3" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="文件名称" prop="fileName"> -->
        <!-- <el-input v-noSpace v-model="formData" placeholder="请输入文件名称"></el-input> -->
        <!-- </el-form-item> -->
        <el-form-item label="使用部门" prop="department">
          <el-input v-noSpace v-model="formData.department" placeholder="请输入使用部门" />
        </el-form-item>

        <el-form-item label="生效时间" prop="effectiveTime">
          <el-date-picker v-model="formData.effectiveTime" type="datetime" />
        </el-form-item>
        <el-form-item label="失效时间" prop="expiryTime">
          <el-date-picker v-model="formData.expiryTime" type="datetime" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-noSpace v-model="formData.remark" type="textarea" />
        </el-form-item>
        <el-form-item label="附件" prop="file">
          <el-upload
            ref="uploadRef"
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :limit="3"
            :auto-upload="false"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <template #tip>
              <div class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button v-debounce="handleSubmit" type="primary">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { Plus, Search, Document } from "@element-plus/icons-vue";
import { ElMessage, UploadInstance } from "element-plus";

import FileManageAPI, {
  fileQuery,
  resultFileInfo,
  uploadFileInfo,
} from "@/api/fileManagement/fileManagement";

const uploadRef = ref<UploadInstance>();
const queryFormRef = ref();
const total = ref(4);

const queryParams = reactive<fileQuery>({
  pageNum: 1,
  pageSize: 10,
});

const tableData = ref<resultFileInfo[]>([]);

const dialogVisible = ref(false);
const formData = reactive<uploadFileInfo>({
  file: undefined,
  fileType: "",
  department: "",
  remark: "",
  effectiveTime: "",
  expiryTime: "",
});

const downloadFile = (row: (typeof tableData.value)[0]) => {
  FileManageAPI.downloadFile(row.filePath).then(() => {
    ElMessage.success(`下载文件：${row.fileName}`);
  });
};

const voidFile = (row: (typeof tableData.value)[0]) => {
  FileManageAPI.voidFile(row.id)
    .then(() => {
      ElMessage.success(`作废文件：${row.fileName}`);
    })
    .then(() => {
      handleQuery();
    });
};

const handleSubmit = () => {
  console.log(formData);
  uploadRef.value!.submit();
  // FileManageAPI.uploadFile(formData)
  //   .then(() => {
  //     ElMessage.success("上传成功");
  //   })
  //   .then(() => {
  //     dialogVisible.value = false;
  //     handleQuery();
  //   });
};

const handleQuery = () => {
  // 这里可以添加查询逻辑，例如调用API获取数据
  FileManageAPI.getFileList(queryParams).then((res) => {
    console.log(res);
    tableData.value = res.list;
    total.value = res.total;
  });
};

const handleResetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
};
onMounted(() => {
  handleQuery();
});
</script>

<style scoped>
.el-input :deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #e5e7eb inset;
}

.el-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1677ff inset;
}

.el-select :deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #e5e7eb inset;
}

.el-select :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1677ff inset;
}

.pagination :deep(.el-pager li.is-active) {
  background-color: #1677ff;
}

.pagination :deep(.el-pagination button:hover) {
  color: #1677ff;
}

.pagination :deep(.el-pager li:hover) {
  color: #1677ff;
}
</style>
